package com.lcz.googleplay.adapter;

import android.graphics.Bitmap;
import android.text.format.Formatter;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.lcz.googleplay.R;
import com.lcz.googleplay.config.BaseProtocol;
import com.lcz.googleplay.domain.HomeInfo;
import com.lcz.googleplay.holder.BaseHolder;
import com.lcz.googleplay.utils.UIUtils;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hp on 2016/10/27.
 */

public abstract class HomeAdapter extends DefaultAdapter<HomeInfo.ListBean> {

    public HomeAdapter(List<HomeInfo.ListBean> list, ListView listView) {
        super(list,listView);
    }

    @Override
    public BaseHolder<HomeInfo.ListBean> getHolder() {
        return new ViewHolder();
    }

    @Override
    protected abstract List<HomeInfo.ListBean> onload();


    static class ViewHolder extends BaseHolder<HomeInfo.ListBean> {
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.ratingBar)
        RatingBar ratingBar;
        @BindView(R.id.tv_size)
        TextView tvSize;
        @BindView(R.id.action_progress)
        FrameLayout actionProgress;
        @BindView(R.id.action_txt)
        TextView actionTxt;
        @BindView(R.id.tv_des)
        TextView tvDes;


        @Override
        public View init() {
            View view = View.inflate(UIUtils.getActivity(), R.layout.item_home, null);
            ButterKnife.bind(this, view);
            return view;
        }

        @Override
        public void refreshView(HomeInfo.ListBean data) {
            //需要开启服务器获取图片
            String imageUri = BaseProtocol.IMAGE_URL + data.getIconUrl();
//            ViewUtils.getImageLoader().displayImage(imageUri, ivIcon, ViewUtils.getOptions());
            SimpleResponseListener<Bitmap> listener = new SimpleResponseListener<Bitmap>() {
                @Override
                public void onSucceed(int what, Response<Bitmap> response) {
                    super.onSucceed(what, response);
                    ivIcon.setImageBitmap(response.get());
                }

                @Override
                public void onFailed(int what, Response<Bitmap> response) {
                    super.onFailed(what, response);
                    ivIcon.setImageResource(R.drawable.ic_default);
                }
            };
            BaseProtocol.loadImage(imageUri, listener);
            tvTitle.setText(data.getName());
            ratingBar.setRating(data.getStars());
            tvSize.setText(Formatter.formatFileSize(UIUtils.getActivity(), data.getSize()));
            tvDes.setText(data.getDes());
        }

    }

}
